package com.jackstraw.jack_base.remote_command.pool;

import cn.hutool.extra.ssh.JschUtil;
import com.jackstraw.jack_base.remote_command.pool.entity.SessiontConnectionConfig;
import com.jcraft.jsch.Session;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

@Data
@Slf4j
public class SessionConnectionFactory extends BasePooledObjectFactory<Session> {

    private SessiontConnectionConfig sessiontConnectionConfig;

    public SessionConnectionFactory(SessiontConnectionConfig sessiontConnectionConfig) {
        this.sessiontConnectionConfig = sessiontConnectionConfig;
    }

    /**
     * 新建对象
     *
     * @return
     * @throws Exception
     */
    @Override
    public Session create() throws Exception {
        if (ObjectUtils.isEmpty(sessiontConnectionConfig)) {
            log.error("初始化Session异常，配置文件为空");
            return null;
        }
        if (!sessiontConnectionConfig.getEnable()) {
            log.warn("初始化Session异常，配置文件启用标记为：{}", sessiontConnectionConfig.getEnable());
            return null;
        }
        Session session = JschUtil.openSession(sessiontConnectionConfig.getIp(), sessiontConnectionConfig.getPort(),
                sessiontConnectionConfig.getUserName(), sessiontConnectionConfig.getPassword(), sessiontConnectionConfig.getTimeout());
        return session;
    }

    @Override
    public PooledObject<Session> wrap(Session obj) {
        return new DefaultPooledObject<>(obj);
    }

    /**
     * 构造一个封装对象
     *
     * @return
     * @throws Exception
     */
    @Override
    public PooledObject<Session> makeObject() throws Exception {
        return super.makeObject();
    }

    /**
     * 销毁对象
     *
     * @param p
     * @throws Exception
     */
    @Override
    public void destroyObject(PooledObject<Session> p) throws Exception {
        Session session = p.getObject();
        if (ObjectUtils.isNotEmpty(session)) {
            if (session.isConnected()) {
                session.disconnect();
            }
        }

    }

    /**
     * 验证对象是否可用
     *
     * @param p
     * @return
     */
    @Override
    public boolean validateObject(PooledObject<Session> p) {
        Session session = p.getObject();
        if (ObjectUtils.isNotEmpty(session)) {
            return session.isConnected();
        }
        return Boolean.FALSE;
    }


    /**
     * 激活一个对象，使其可用用
     *
     * @param p
     * @throws Exception
     */
    @Override
    public void activateObject(PooledObject<Session> p) throws Exception {
        Session session = p.getObject();
        if (ObjectUtils.isNotEmpty(session) && !session.isConnected()) {
            session.connect();
        }
    }

    /**
     * 钝化一个对象,也可以理解为反初始化
     *
     * @param p
     * @throws Exception
     */
    @Override
    public void passivateObject(PooledObject<Session> p) throws Exception {
        super.passivateObject(p);
    }
}
